/* 
    @fiel:  protocol.c
    @code:  c(//utf-8)
 */
/* ------------------------------------------------------------------------- */
#include "protocol.h"

unsigned char ProtocolRun(protocol_t * pptl,unsigned char ich)
{
    if(pptl == 0){return 0;}
    if(pptl->pbuf == 0){return 0;}

    switch (pptl->step)
    {
        case 0:
            pptl->pbuf[0] = pptl->pbuf[1];
            pptl->pbuf[1] = pptl->pbuf[2];
            pptl->pbuf[2] = pptl->pbuf[3];
            pptl->pbuf[3] = ich;

            if( (pptl->pbuf[0] == 0xaa)&&\
                (pptl->pbuf[1] == 0x55)&&\
                (pptl->pbuf[2] == 0x00)&&\
                (pptl->pbuf[3] == 0x11))
            {
                pptl->step = 1;
                pptl->datacnt = 4;
            }
            break;
        case 1:
            pptl->pbuf[pptl->datacnt]  = ich;
            pptl->targcnt = ich+5;
            pptl->datacnt++;
            pptl->step = 2;
            break;
        case 2:
            if((pptl->datacnt)>(pptl->bufsize))
            {
                pptl->step = 0;
                pptl->datacnt = 0;
            }
            pptl->pbuf[pptl->datacnt]  = ich;
            pptl->datacnt++;
            if((pptl->datacnt)>=(pptl->targcnt))
            {
                pptl->step = 0;
                return 1;
            }
            break;
        default:
            break;
    }

    return 0;
}


/* ------------------------------------------------------------------------- */
